import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import AddEditModal from '#cpt/temp/AddEditModal'
import Confirm from '#cpt/temp/Confirm'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			// 项目管理调换搜索字段位置 耿琰 20220627
			{ label: '设备名称',			name: 'device_id',	type: 'select', loading:true, width:250, onValue:option => $fn.searchDevice.call(this,option)  },
			{ label: '设备编号',			name: 'device_number' },
			{ label: '设备型号',			name: 'device_model' },
			{ label: '仪器通道号',		name: 'pass_num' },
			{ label: '单一项目名称',		name: 'kind_id', type: 'select' },
			{ label: '联机状态',			name: 'rel_status',			type: 'select', loading:true, idStr:'code'  },
			{ label:' ', tag: '不显示从属项目',	name: 'hide_sub', 	type:'checkbox', colon:false },
			{ label:' ', tag: '默认',			name: 'is_default', type:'checkbox', colon:false },
		],
		// 提交表单列表
		formData : [
			{ label: '设备名称:',	name: 'device_name',		half:true, disabled:true },
			{ label: '设备编号',		name: 'device_number', 		half:true, disabled:true },
			{ label: '通道号',		name: 'pass_num', 			full:true, required:true, type:'select', loading:true, idStr:'pass_num', onValue:v=>$fn.setRowValue.call(this,'device_pro_name',v)},
			{ label: '仪器项目名称',	name: 'device_pro_name', 	full:true, required:true, disabled:true },
		]
	}
	
	model = {}
	resetModel = { }
	
	componentDidMount(){
		const { kind_id, device_id, device_name } = $fn.query()
		if(device_id){ 
			this.query = { kind_id, device_id, device_name: $fn.decode(device_name) }
			this.resetModel = { device_id, device_name: $fn.decode(device_name) }
			this.device_id = device_id
		}
		
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 设备名称
			$fn.setRow(forms, 'device_id', await f.getDivCache() )
			// 设置默认值
			if(this.device_id){ this.searchFormRef.setValue({ ...this.resetModel }) }
			// 联机状态
			$fn.setRow(forms, 'rel_status', await f.getDicCache({ code:69600 }) )
			// 项目名称
			$fn.setRow( forms, 'kind_id', await f.getCache({ api:'labkindselect' } ) )
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'de-rel-kind/index')
	
	// 表单
	cols = [
		{ title: '编号',				dataIndex: 'device_number', 	width:120 },
		{ title: '设备型号', 		dataIndex: 'device_model', 		width:150 },
		{ title: '设备名称', 		dataIndex: 'device_name', 		width:150 },
		{ title: '关联项目代码', 		dataIndex: 'kind_code', 		width:100 },
		{ title: '关联项目名称', 		dataIndex: 'kind_name', 		width:250 },
		{ title: '单一项目', 		dataIndex: 'single_kind_name', 	width:250 },
		{ title: '结果类型', 		dataIndex: 'result_type_name', 	width:150 },
		{ title: '仪器通道号', 		dataIndex: 'pass_num', 			width:150, 	sorter:true },
		{ title: '是否默认', 		dataIndex: 'is_default', 		width:100, 	align:'center', render: text => +text === 1 ? '是' : '否' },
		{ title: '联机状态', 		dataIndex: 'rel_status_name', 	width:100, 	align:'center'  },
		{ title: '仪器关联状态', 		dataIndex: 'enabled_name', 		width:100, 	align:'center'  },
		TableControls.controls.call(this, {width:220})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, device_id, enabled }) => [
		TableControls.edit.call(this, 'de-rel-kind/info', { uuid }, {
			label:'编辑通道号',
			select: ()=> {
				const { formData } = this.state
				return $http.pull(null, 'de-pass-info/index', { param:{ device_id, limit:10000 } } ).then(data=>{
					if($fn.hasArray(data)){
						// _data.forEach(v => v.name = v.device_model + ' - ' + v.device_pro_name)
						data.forEach(v => v.name = v.pass_num + ' - ' + v.device_pro_name)
						// 修改选择内容为仪器通道号+名称
						$fn.setRow(formData,'pass_num', data)
					}
				})
			}
		}),								// 编辑
		this.onDel([uuid]),				// 删除
		// 下线设备
		+enabled === 1 && Confirm.call(this, '下线设备', 'de-rel-kind/off', { param: { uuid }, isKeys: !uuid }),
		+enabled === -2 && Confirm.call(this, '开启设备', 'de-rel-kind/open', { param: { uuid }, isKeys: !uuid })
		
	]
	// 删除
	onDel = uuid => Delete.call(this, 'de-rel-kind/del', { uuid, isKeys: !uuid })
	// 下线设备
	// onOff = uuid => Confirm.call(this, '下线设备', 'de-rel-kind/off', { param: { uuid }, isKeys: !uuid })
	
	// 操作按钮
	controls = () => [
		this.onLink(69601),
		this.onLink(69603),
		this.device_id && Controls.back.call(this)
	]
	
	// 项目联机
	onLink = rel_status => Confirm.call(this,rel_status === 69601 ? '项目联机':'取消联机', 'de-rel-kind/setStatus', { param: keys=>({ uuids: keys, rel_status  }), isKeys: true })
	
	render(){
		return (
			<>
				<Page title='项目关联' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm: this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this,{sort:true, isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ title:'通道号', labelWidth:'s6', editApi:'de-rel-kind/edit' }) }
			</>
		)
	}
}

export default Index